package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.activity.DaggerRecrdActivityComponent;
import com.kucai.yifaqapp.dagger.module.activity.RecrdActivityModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.ResCording;
import com.kucai.yifaqapp.presenter.activity.RecrdActivityPresenter;
import com.kucai.yifaqapp.ui.view.RecrdActivityView;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.util.PriceUtils;

import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import static com.kucai.yifaqapp.global.App.getContext;

/**
 * 下注记录
 */
public class RecrdActivity extends BaseActivity<RecrdActivityPresenter> implements RecrdActivityView {

    LinearLayout titleLeftTv;
    TextView headerTitle;
    TextView tip;
    RecyclerView recrdlist;
    SwipeRefreshLayout srl;
    TextView numxiazhu;
    TextView numwin;
    RelativeLayout skinbg;
    private ArrayList<ResCording.DataBean> crdata = new ArrayList<>();
    private Adapter adapter;
    private String starttime;
    private String endtime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleLeftTv = findViewById(R.id.goback);
        headerTitle = findViewById(R.id.headerTitle);
        recrdlist = findViewById(R.id.recrdlist);
        srl = findViewById(R.id.srl);
        numxiazhu = findViewById(R.id.numxiazhu);
        numwin = findViewById(R.id.numwin);
        skinbg = findViewById(R.id.skinbg);
        tip = findViewById(R.id.tip);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        endtime = sdf.format(date);

        tip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前时间
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date curDate = new Date(System.currentTimeMillis());
                String str = formatter.format(curDate);
                //获取当日下注记录
                Intent intent = new Intent(RecrdActivity.this, RecrdActivity1.class);
                intent.putExtra("statDate", str);
                startActivity(intent);
            }
        });

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        Date date2 = new Date();
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        calendar2.add(Calendar.DAY_OF_MONTH, -8);
        date2 = calendar2.getTime();
        starttime = sdf2.format(date2);

        mPresenter.getrecording(Info.user.token, starttime, endtime);
        headerTitle.setText("下注记录");
        titleLeftTv.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });

        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getrecording(Info.user.token, starttime, endtime);
            }
        });

        recrdlist.setLayoutManager(new LinearLayoutManager(this));
        adapter = new Adapter();
        recrdlist.setAdapter(adapter);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_recrd;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    @Override
    protected void initDagger() {
        DaggerRecrdActivityComponent.builder()
                .recrdActivityModule(new RecrdActivityModule(this))
                .build().inject(this);
    }

    @Override
    public void getCording(ResCording resCording) {
        try {
            srl.setRefreshing(false);
            crdata.clear();
            crdata.addAll(resCording.data);
//            java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
//            df.setRoundingMode(RoundingMode.FLOOR);
            String d1 = cutDoubleNumber(resCording.otherData.totalBetCount);
            String d2 = cutDoubleNumber(resCording.otherData.totalBetMoney);
            numxiazhu.setText(d1);
            numwin.setText(d2);
            adapter.notifyDataSetChanged();
        } catch (Exception e) {

        }
    }


    public static String cutDoubleNumber(Double number) {
//
        java.text.DecimalFormat df = new java.text.DecimalFormat("0.00");
        df.setRoundingMode(RoundingMode.FLOOR);
        String d = df.format(number);

        //四舍五入保留两位小数,number.toString()是一个Double值
//        double v = new BigDecimal(number.toString()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return d;
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.recarditem, parent, false));
        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            holder.recardtime.setText(crdata.get(position).statDate);
            holder.recarddown.setText(crdata.get(position).weekTime);
            holder.recardnum.setText(crdata.get(position).betCount + "");
            holder.recardwin.setText(crdata.get(position).rewardRebate + "");
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (crdata.get(position).betCount == 0) {
                        return;
                    } else {
                        Intent intent = new Intent(RecrdActivity.this, RecrdActivity1.class);
                        intent.putExtra("statDate", crdata.get(position).statDate);
                        startActivity(intent);
                    }
                }
            });
            PriceUtils.format(holder.recardwin);
        }

        @Override
        public int getItemCount() {
            return crdata.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView recardnum, recardtime, recardwin, recarddown;

            MViewHolder(View itemView) {
                super(itemView);
                recardnum = (TextView) itemView.findViewById(R.id.recardnum);
                recardtime = (TextView) itemView.findViewById(R.id.recardtime);
                recardwin = (TextView) itemView.findViewById(R.id.recardwin);
                recarddown = (TextView) itemView.findViewById(R.id.recarddown);

            }
        }
    }


}

